package com.hoshiicloud.user.controller.partners;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.partners.PlatPartners;
import com.hoshiicloud.user.param.partners.PlatPartnersAddParam;
import com.hoshiicloud.user.param.partners.PlatPartnersQueryParam;
import com.hoshiicloud.user.param.partners.PlatPartnersUpdateParam;
import com.hoshiicloud.user.service.partners.PlatPartnersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author:campchen
 * @Date:Created in 下午 07:05 2019/7/18
 * @PACKAGENAME controller
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/partner/plat-partner")
@Api(value = "合作伙伴表信息", tags = {"平台 合作伙伴管理"})
public class PlatPartnersController {
    @Autowired
    private PlatPartnersService platPartnersService;


    @ApiOperation(value = "分页查询平台合作伙伴")
    @PostMapping(value = "/getPage")
    public Response<IPage<PlatPartnersQueryParam>> getPage(@RequestBody @Valid PlatPartnersQueryParam param, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
        }
        QueryWrapper<PlatPartners> queryWrapper = new QueryWrapper<PlatPartners>();
        queryWrapper.eq(param.getPlatid() != null && param.getPlatid() != 0, "company_id", param.getPlatid());
        queryWrapper.eq(StringUtils.isNotBlank(param.getOrgcode()), "orgcode", param.getOrgcode());
        queryWrapper.eq(param.getDepartmentId() != null && param.getDepartmentId() != 0, "department_id", param.getDepartmentId());
        queryWrapper.eq(param.getMemberid() != null && param.getMemberid() != 0, "memberid", param.getMemberid());
        queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getCharge()), "charge", param.getCharge());
        queryWrapper.eq(StringUtils.isNotBlank(param.getIdCard()), "id_card", param.getIdCard());
        queryWrapper.eq(StringUtils.isNotBlank(param.getBankNumber()), "bank_number", param.getBankNumber());
        queryWrapper.eq(StringUtils.isNotBlank(param.getBankName()), "bank_name", param.getBankName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getBankAddress()), "bank_address", param.getBankAddress());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTaxation()), "taxation", param.getTaxation());
        queryWrapper.eq(StringUtils.isNotBlank(param.getPiaohao()), "piaohao", param.getPiaohao());
        queryWrapper.eq(StringUtils.isNotBlank(param.getGoodsAddress()), "goods_address", param.getGoodsAddress());
        queryWrapper.eq(StringUtils.isNotBlank(param.getWhouseAddress()), "whouse_address", param.getWhouseAddress());
        queryWrapper.eq(param.getPaymentId() != null && param.getPaymentId() != 0, "payment_id", param.getPaymentId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTelphone()), "telphone", param.getTelphone());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMobile()), "mobile", param.getMobile());
        queryWrapper.eq(StringUtils.isNotBlank(param.getFax()), "fax", param.getFax());
        queryWrapper.eq(StringUtils.isNotBlank(param.getHome()), "home", param.getHome());
        queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());
        queryWrapper.eq(StringUtils.isNotBlank(param.getAddress()), "address", param.getAddress());
        queryWrapper.eq(StringUtils.isNotBlank(param.getCreatedby()), "createdby", param.getCreatedby());
        queryWrapper.eq(StringUtils.isNotBlank(param.getModifiedby()), "modifiedby", param.getModifiedby());
        queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
        queryWrapper.eq(param.getType() != null, "type", param.getType());
        queryWrapper.eq(param.getAvailableBalance() != null, "availableBalance", param.getAvailableBalance());
        queryWrapper.eq(param.getAccountBalance() != null, "accountBalance", param.getAccountBalance());
        queryWrapper.eq(param.getFreezingAmount() != null, "freezingAmount", param.getFreezingAmount());
        queryWrapper.select(BeanUtils.getSqlSelect(PlatPartners.class));
        IPage<PlatPartners> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        return platPartnersService.getPage(page, queryWrapper, param.toString() + param.getPageNumber() + param.getPageSize());
    }

    @GetMapping("/{id}")
    @ApiParam(name = "id", value = "主键ID", required = true)
    @ApiOperation(value = "根据id查询平台合作伙伴")
    public Response getPlatPartnersById(@PathVariable("id") Long id) {
        return Response.data(platPartnersService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增平台合作伙伴")
    public Response savePlatPartners(@RequestBody @Valid PlatPartnersAddParam param) {
        PlatPartners data;
        try {
            data = BeanUtils.covertType(param, PlatPartners.class);
        } catch (Exception e) {
            log.error("新增平台合作伙伴异常", e);
            return Response.failed(e.getMessage());
        }
        return Response.data(platPartnersService.insert(data));
    }

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改合作伙伴
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台合作伙伴")
    public Response update(@RequestBody @Valid PlatPartnersUpdateParam param) {
        PlatPartners data;
        try {
            data = BeanUtils.covertType(param, PlatPartners.class);
        } catch (Exception e) {
            log.error("修改平台合作伙伴异常", e);
            return Response.failed(e.getMessage());
        }
        return Response.data(platPartnersService.updateById(data));
    }

}
